package org.baecks.gwt.universalplayer.client;

import java.util.HashMap;
import java.util.Map;

import com.bramosystems.oss.player.core.client.AbstractMediaPlayer;
import com.bramosystems.oss.player.core.client.LoadException;
import com.bramosystems.oss.player.core.client.PlayException;
import com.bramosystems.oss.player.core.client.PlayerUtil;
import com.bramosystems.oss.player.core.client.Plugin;
import com.bramosystems.oss.player.core.client.PluginNotFoundException;
import com.bramosystems.oss.player.core.client.PluginVersionException;
import com.bramosystems.oss.player.core.event.client.DebugEvent;
import com.bramosystems.oss.player.core.event.client.DebugHandler;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.SimplePanel;

public class UniversalPlayer extends SimplePanel {
	Map<Plugin, AbstractMediaPlayer> playerMap = new HashMap<Plugin, AbstractMediaPlayer>();
	AbstractMediaPlayer activePlayer = null;
	Plugin activePlugin = null;
	
	String width = "100px";
	String height = "100px";
	boolean debug = false;

	public UniversalPlayer(String width, String height, boolean debug) {
		super();
		this.setSize(width, height);
		
		this.width = width;
		this.height = height;
		this.debug = debug;
	}

	public void stop(){
		if(activePlayer == null){
			return;
		}
		
		try{
			activePlayer.stopMedia();
		} catch(Throwable t){
			System.out.println(t.getMessage());
		}
	}
	
	public void play() throws PlayException{
		if(activePlayer == null){
			return;
		}
		
		activePlayer.playMedia();
	}

	public void pause(){
		if(activePlayer == null){
			return;
		}
		
		activePlayer.pauseMedia();
	}
	
	public void fullscreen(boolean full){
		if(activePlayer == null){
			return;
		}	
	}
	
	public void loadAndPlay(String url) throws LoadException{
		stop();
		//this.clear();
		
		AbstractMediaPlayer currentPlayer = activePlayer;
		
		if(url == null){
			setErrorMessageWidget("No player can be activated for a null URL!");
			throw new LoadException("No player can be activated for a null URL!");
		}
		
		if((activePlugin != null) && (PlayerUtil.canHandleMedia(activePlugin, url))){
			try{
				currentPlayer.loadMedia(url);
				return;
			} catch(Throwable t){
				System.out.println(t);
			}
		}
		
		String exceptionMessage = "No player found for: " + url + "\n";
		
		AbstractMediaPlayer player = null;
		
		for(Plugin p: Plugin.values()){
			if(!PlayerUtil.canHandleMedia(p, url)){
				continue;
			}
			
			activePlugin = p;
			
			//we found a plugin capable of playing the content
			/*player = playerMap.get(p);
			if(player != null){
				break;
			}
			 */
			
			try {
				player = PlayerUtil.getPlayer(p, url, true, width, height);
				/*if(player instanceof FlashMediaPlayer){
					((FlashMediaPlayer)player).setShuffleEnabled(true);
					((FlashMediaPlayer)player).setControllerVisible(false);
				}*/
/*							
				player.addPlayerStateHandler(new PlayerStateHandler(){

					@Override
					public void onPlayerStateChanged(PlayerStateEvent e) {
						if (e.getPlayerState().equals(State.ACTIVE)){
							
						}
						
					}				
				});*/
				
				//return;
			} catch (LoadException e) {
				exceptionMessage += "Failed to load a player for plugin " + p.name() + " (" + e.getMessage() + ")\n";
				continue;
			} catch (PluginNotFoundException e) {
				exceptionMessage += "Plugin " + p.name() + " couldn't be found (" + e.getMessage() + ")\n";
				continue;
			} catch (PluginVersionException e) {
				exceptionMessage += "Plugin " + p.name() + " v" +
				e.getRequiredVersion() + " is required, but only v" + e.getVersionFound() + " is found!\n";
				continue;
			}
				
			if(player == null){
				exceptionMessage += "Failed to create a player for plugin " + p.name() + ")\n";
				continue;
			}
			
			if(debug){
				player.addDebugHandler(new DebugHandler(){
					public void onDebug(DebugEvent event) {
						System.out.println("UniversalPlayer: " + event.getMessage());										
					}
				});
			}
			// playerMap.put(p, player);
			break;
		}
		
		if(player == null){
			setErrorMessageWidget(exceptionMessage);
			throw new LoadException(exceptionMessage);
		}
		
		activePlayer = player;
		this.clear();
		this.setWidget(player);
	}
	
	private void setErrorMessageWidget(String error){
		activePlugin = null;
		activePlayer = null;
		HTML w = new HTML("<DIV class=\"upErrorWidget\">" + error.replace("\n", "<BR>") + "</DIV>");
		w.setSize(this.width, this.height);
		this.clear();
		this.setWidget(w);
	}
}
